package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.school.entity.SchoolTeacherIcard;

public class SchoolTeacherIcardRowMapper implements RowMapper<SchoolTeacherIcard> {

    @SuppressWarnings("unused")
    private int type;

    public static final int AND_USERINFO = 1;
    
    public SchoolTeacherIcardRowMapper() {
    }

    public SchoolTeacherIcardRowMapper(int type) {
        this.type = type;
    }

    @Override
    public SchoolTeacherIcard mapRow(ResultSet rs, int rowNum) throws SQLException {
    	switch (type) {
            case AND_USERINFO:
                SchoolTeacherIcard sti = parseSchoolTeacherIcard(new SchoolTeacherIcard(), rs, "sti");
                sti.setIcname(rs.getString("icname"));
                sti.setIcfoot(rs.getString("icfoot"));
                sti.setRealname(rs.getString("realname"));
                sti.setSname(rs.getString("sname"));
                sti.setBsdate(rs.getTimestamp("bsdate"));
                sti.setState(rs.getShort("state"));
                return sti;
        }
        return parseSchoolTeacherIcard(new SchoolTeacherIcard(), rs);
    }

    public static SchoolTeacherIcard parseSchoolTeacherIcard(SchoolTeacherIcard si, ResultSet rs) throws SQLException {
        return parseSchoolTeacherIcard(si, rs, null);
    }

    public static SchoolTeacherIcard parseSchoolTeacherIcard(SchoolTeacherIcard si, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try{
            si.setStid(rs.getLong(su+"stid"));
        }catch(SQLException e){
            si.setStid(null);
        }
        try {
            si.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            si.setSid(null);
        }
        try {
            si.setIcid(rs.getLong(su+"icid"));
        } catch (SQLException e) {
            si.setIcid(null);
        }
        try {
            si.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            si.setTid(null);
        }
        return si;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, SchoolTeacherIcard si) {
        par.put("tid", si.getTid());
        par.put("sid",si.getSid());
        par.put("icid",si.getIcid());
        
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, SchoolTeacherIcard si) {
        args.put("stid", si.getStid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final SchoolTeacherIcard si) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(si),
                        new String[] { "stid" });
                parsePS(ps, si);
                return ps;
            }

        };
    }

    private static String parseSQL(SchoolTeacherIcard si) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into school_teacher_icard(");
        int count = 0;
        if (null != si.getSid()) {
            sql.append("sid,");
            count++;
        }
        if (null !=si.getIcid()) {
            sql.append("icid,");
            count++;
        }
        if (null != si.getTid()) {
            sql.append("tid,");
            count++;
        }

        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, SchoolTeacherIcard si)
        throws SQLException {
        int count = 1;
        if (null != si.getSid()) {
            ps.setInt(count++,si.getSid());
        }    
        if (null != si.getIcid()) {
            ps.setLong(count++, si.getIcid());
        }
        if (null != si.getTid()) {
            ps.setLong(count++,si.getTid());
        }
        
    }

}
